<?php
defined('_JEXEC') or die();

jimport('joomla.application.component.controllerform');
require_once JPATH_COMPONENT.DS.'helpers'.DS.'ftpHelper.php';

class Easiv2adminControllerManageInstances extends JControllerForm {
	
	function display() {
		JHTML::stylesheet('easiv2.css', JPATH_COMPONENT.DS.'css');
		
		parent::display();
	}
	
	function cancel() {
		JRequest::setVar('view', 'manageInstances');
		
		parent::display();
	}
	
	function redirect() {
		parent::redirect();
	}
	
	function gotoAddExistingInstance() {		
		JRequest::setVar('view', 'addExistingInstance');
		
		parent::display();
	}
	
	function gotoEditInstances() {
		JRequest::setVar('view', 'editInstances');
		
		parent::display();
	}
	
	function gotoExtensions() {
		JRequest::setVar('view', 'manageExtensions');
		
		parent::display();
	}
	
	/**
	 * Validation and saving to database
	 */
	function saveInstance() {
//		/*
//		 * setting default values if none were inserted by user
//		 */
//		if(($name = $_POST['name']) == "")
//			$name = 'Default Site';
//		
//		if(($port = $_POST['port']) == "")
//			$port = 21;
		
		$name = JRequest::getVar('name');
		$port = JRequest::getVar('port');
		$http_address = JRequest::getVar('http_address');
		$ftp_host = JRequest::getVar('ftp_host');
		$host_folder = JRequest::getVar('host_folder');
		$ftp_user = JRequest::getVar('ftp_user');
		$ftp_pass = JRequest::getVar('ftp_pass');
		
		/*
		 * check and modify userinput for correct syntax if nescessary
		 */
		if(substr($http_address, strlen($http_address)-1) != '/') {
			$http_address .= '/';
		}
		
		if(substr($http_address, 0, 7) != "http://") {
			$http_address = 'http://'.$http_address;
		}
		
		if($ftp_user != "" && $ftp_pass != "" && $http_address != "" && $ftp_host != "") {
			$ftp = new FtpHelper();
			
			if($ftp->isFtpValid($ftp_host, $port, $ftp_user, $ftp_pass)) {			
				$db = JFactory::getDBO();
				$query = "INSERT INTO #__easiv2_instances VALUES (DEFAULT, '".$name."', '".$http_address."', '".$ftp_host."', '".$port."', '".$host_folder."', '".$ftp_user."', '".$ftp_pass."');";
				$db->setQuery($query);
				$db->query();
				
				JRequest::setVar('view', 'manageInstances');
				
				echo "Instance has been added <span style=\"color:#0F0\"><b>successfully</b><br></span>";
			}
			else {
				echo "<span style=\"color:#F00\"><b>Cannot connect to FTP.</b></span>";
			}
		}
		else {
			echo "<span style=\"color:#F00\"><b>Some information is missing. Please check again.</b></span>";
		}
		
		parent::display();
	}
	
	function deleteInstances() {
		$idToDelete = JRequest::getVar('cid');
		$db =& JFactory::getDBO();
		
		foreach($idToDelete as $id) {
			$db->setQuery("DELETE FROM #__easiv2_instances WHERE id='".$id."'");
			if($db->query() != false) {
				echo "Deleted Instance with ID <b>".$id."</b> successfully!<br>";
			}
		}		
		
		Jrequest::setVar('view', 'manageInstances');
		parent::display();
	}
}